package com.atguigu.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author:
 * @Date: 2023/8/15 14:50
 * @Description: 添加描述
 */
@Configuration
public class MyCorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration configuration = new CorsConfiguration();
//        configuration.addAllowedOrigin("http://localhost:8888");// 允许谁跨域
        configuration.addAllowedOrigin("*");// 允许所有人跨域
        configuration.setAllowCredentials(true);// 传cookie
        configuration.addAllowedMethod("*");// 允许哪些方法跨域 post get
        configuration.addAllowedHeader("*");// 允许哪些头信息

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);// 拦截一切请求

        return new CorsFilter(source);
    }
}
